

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="前言之前介绍了pytest的基本使用, 以及pytest常用的一些插件 实际上, pytest本身还具备了一些十分好用的功能, 可以有效地帮助我们完成自动化框架的封装 这些功能在我介绍后,可能会让大家眼前一亮 这篇文章会介绍下列功能:  跳过执行 用例标记 数据驱动  以上介绍的都是pytest内置的功能, 所以不需要额外安装任何第三方库 跳过执行pytest.skip&#x2F;@pytest.mark.">
<meta property="og:type" content="article">
<meta property="og:title" content="pytest进阶使用">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2023/05/11/autoTest/pytest%E8%BF%9B%E9%98%B6%E4%BD%BF%E7%94%A8/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="前言之前介绍了pytest的基本使用, 以及pytest常用的一些插件 实际上, pytest本身还具备了一些十分好用的功能, 可以有效地帮助我们完成自动化框架的封装 这些功能在我介绍后,可能会让大家眼前一亮 这篇文章会介绍下列功能:  跳过执行 用例标记 数据驱动  以上介绍的都是pytest内置的功能, 所以不需要额外安装任何第三方库 跳过执行pytest.skip&#x2F;@pytest.mark.">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-05-11T06:18:11.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.246Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="自动化测试">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="自动化测试框架">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>pytest进阶使用 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="pytest进阶使用"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-05-11 14:18" pubdate>
          2023年5月11日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          8.6k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">pytest进阶使用</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>之前介绍了pytest的基本使用, 以及pytest常用的一些插件</p>
<p>实际上, pytest本身还具备了一些十分好用的功能, 可以有效地帮助我们完成自动化框架的封装</p>
<p>这些功能在我介绍后,可能会让大家眼前一亮</p>
<p>这篇文章会介绍下列功能:</p>
<ul>
<li>跳过执行</li>
<li>用例标记</li>
<li>数据驱动</li>
</ul>
<p>以上介绍的都是pytest内置的功能, 所以不需要额外安装任何第三方库</p>
<h1 id="跳过执行"><a href="#跳过执行" class="headerlink" title="跳过执行"></a>跳过执行</h1><p><code>pytest.skip/@pytest.mark.skip</code></p>
<p>主要是用来跳过执行某条用例, 看起来很像是 <strong>常用插件中的pytest-dependency(用例依赖)</strong></p>
<p>但这个是直接跳过, 不依赖于任何条件</p>
<p>比如, 在某个需求版本中, 尚未实现某个接口的功能, 但我们已经根据需求/接口文档编写好了自动化用例</p>
<p>那我们执行的时候, 就可以通过 <code>skip</code> 跳过它</p>
<p>并且, 跳过时, 可以写上跳过原因, 起到了很好的告示作用.</p>
<h2 id="pytest-skip"><a href="#pytest-skip" class="headerlink" title="pytest.skip"></a>pytest.skip</h2><p>在函数执行过程中直接调用, 调用后, 其下的代码不会被执行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a1&quot;</span>)<br>        pytest.skip(reason=<span class="hljs-string">&quot;跳过此次执行&quot;</span>)  <span class="hljs-comment"># 跳过执行, 并声明跳过原因</span><br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a2&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -v 可以看到跳过原因</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果:</span><br><span class="hljs-string">collected 2 items                                                                                                                                                                                                                  </span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a -------&gt;test_a1</span><br><span class="hljs-string">SKIPPED (跳过此次执行)</span><br><span class="hljs-string">test3.py::TestCase::test_b -------&gt;test_b</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string"></span><br><span class="hljs-string">1 passed, 1 skipped</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现, print(&quot;-------&gt;test_a2&quot;), 因为跳过的原因, 并没有被执行</span><br><span class="hljs-string">并且我们可以在结果中看到跳过原因</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h2 id="pytest-mark-skip"><a href="#pytest-mark-skip" class="headerlink" title="@pytest.mark.skip"></a>@pytest.mark.skip</h2><p>通过装饰器的方式, 实现跳过用例</p>
<p>被该装饰器装饰的用例, 不会被执行</p>
<p>同样的, 可以写上跳过原因</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.skip(<span class="hljs-params">reason=<span class="hljs-string">&quot;跳过此次执行&quot;</span></span>)</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a1&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a2&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -v 可以看到跳过原因</span><br></code></pre></td></tr></table></figure>

<p>你还可以跳过某个测试类, 同样使用这个装饰器, 不过是放在类上</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-meta">@pytest.mark.skip(<span class="hljs-params">reason=<span class="hljs-string">&quot;跳过此次执行&quot;</span></span>)</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br>    <br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a1&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a2&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -v 可以看到跳过原因</span><br></code></pre></td></tr></table></figure>

<h1 id="用例标记"><a href="#用例标记" class="headerlink" title="用例标记"></a>用例标记</h1><p>标记用例是通过装饰器: <code>@pytest.mark</code> 来实现的</p>
<p>如果看过 <strong>常用插件</strong> 那一篇文章, 大家对这个应该不会太陌生</p>
<p>本质上, 这个装饰器就是为了给用例打上标记, 然后供pytest识别</p>
<p>所以第三方插件的实现方式, 本质上也是通过识别 <strong>用例标签</strong> 来进行不同功能的实现的.</p>
<p>当然, pytest肯定也会有内置的功能, 来识别用例标签.</p>
<p>这个功能就是: <strong>控制那些标签用例可以运行, 或者说跳过某些标签用例运行</strong></p>
<p>可以想象:</p>
<blockquote>
<p>你编写了很多自动化用例, 这些自动化用例, 按照优先级/环境, 可能会有不同的标签<br>你希望根据不同的情况运行不同的用例<br>比如, 你希望在版本迭代测试开始前, 运行冒烟自动化用例, 标签为 <strong>level0</strong> 或者是 <strong>smoke</strong><br>比如, 你希望在测试环境运行测试环境应该运行的自动化用例, 生产环境运行生成环境的用例, 标签可能为 <strong>test</strong> 和 <strong>prod</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.test  </span><span class="hljs-comment"># 打上标签 test</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, <span class="hljs-string">&#x27;-m test&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果:</span><br><span class="hljs-string"></span><br><span class="hljs-string">collected 2 items / 1 deselected / 1 selected                     </span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a -------&gt;test_a</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string"></span><br><span class="hljs-string">1 passed, 1 deselected</span><br><span class="hljs-string"></span><br><span class="hljs-string">从结果可以发现, 我们只运行了 test用例</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.test</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-meta">    @pytest.mark.dev</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_c</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_c&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, <span class="hljs-string">&#x27;-m test or dev&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">结果:</span><br><span class="hljs-string"></span><br><span class="hljs-string">collected 3 items / 1 deselected / 2 selected</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a -------&gt;test_a</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_c -------&gt;test_c</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string"></span><br><span class="hljs-string">2 passed, 1 deselected</span><br><span class="hljs-string"></span><br><span class="hljs-string">从结果可以发现, 我们可以通过 or  来运行多个标签用例</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.test</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-meta">    @pytest.mark.dev</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_c</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_c&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, <span class="hljs-string">&#x27;-m not test&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 3 items / 1 deselected / 2 selected</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_b -------&gt;test_b</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_c -------&gt;test_c</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string"></span><br><span class="hljs-string">还可以通过not 来控制不运行的标签用例</span><br><span class="hljs-string"></span><br><span class="hljs-string">除了test的用例都会运行</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>当然, 你还可以标记一个测试类.</p>
<p>当然, 也会有标记的测试类下的方法继续被标记的情况</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-meta">@pytest.mark.test</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_a&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_b&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-meta">    @pytest.mark.dev</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_c</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-------&gt;test_c&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span> <br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, <span class="hljs-string">&#x27;-m dev or not test&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 3 items / 2 deselected / 1 selected</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_c -------&gt;test_c</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string"></span><br><span class="hljs-string">可以发现, 被 test 标记的测试类下, 除了 test_c 都被执行了</span><br><span class="hljs-string"></span><br><span class="hljs-string">原因就是我们 使用的是 dev or not test  而 test_c 就是被dev标签标记了</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h1 id="数据驱动"><a href="#数据驱动" class="headerlink" title="数据驱动"></a>数据驱动</h1><p>数据驱动, 在pytest中的实现十分简单, 并且通用具备了pytes的特色, 那就是灵活</p>
<p>大家可能对 <strong>数据驱动</strong> 这个概念比较模糊, 其实我说一个场景, 大家就明白了.</p>
<blockquote>
<p>有一个数据文件, 需要对里面的文件的每一行数据, 进行拆分, 每一行即是一条用例<br>这个文件的数据动态的, 行数/内容, 也许每天都不同<br>这个时候, 如果我们按照之前那样, 编写一个个test函数, 就无法实现这个需求</p>
</blockquote>
<p>数据驱动, 就是为了解决类似于这样的问题</p>
<p>这里我们需要使用到 <code>@pytest.mark.parametrize</code></p>
<h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-comment"># 模拟文件内容, 将文件内容按行拆分</span><br>fileData = [<br>    <span class="hljs-string">&quot;data1&quot;</span>,<br>    <span class="hljs-string">&quot;data2&quot;</span>,<br>    <span class="hljs-string">&quot;data3&quot;</span>,<br>    <span class="hljs-string">&quot;data4&quot;</span>,<br>]<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.parametrize(<span class="hljs-params"><span class="hljs-string">&#x27;data&#x27;</span>, fileData</span>)  </span><span class="hljs-comment"># 声明参数名以及确定被测数据</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self, data</span>):  <span class="hljs-comment"># 申明同名函数data</span><br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data: <span class="hljs-subst">&#123;data&#125;</span>&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 4 items</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a[data1] data: data1</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[data2] data: data2</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[data3] data: data3</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[data4] data: data4</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>可以发现, <strong>test_a</strong> 函数, 被运行了 <strong>四次</strong></p>
<p>因为我们给到的数据一共有 <strong>四份</strong></p>
<p><code>@pytest.mark.parametrize</code>会将被装饰器的函数, 会执行相对应的数据份数</p>
<ul>
<li>参数1: 被测函数传入的函数名称</li>
<li>参数2: 被测数据, 可以是数组/元组等可以被循环的数据</li>
</ul>
<p>被测函数, 只需要申明 <strong>同名参数</strong> 即可</p>
<h2 id="多参数"><a href="#多参数" class="headerlink" title="多参数"></a>多参数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-comment"># 模拟文件内容, 将文件内容按行拆分</span><br>fileData = [<br>    (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>),<br>    (<span class="hljs-number">2</span>, <span class="hljs-number">3</span>),<br>    (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>),<br>]<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.parametrize(<span class="hljs-params">[<span class="hljs-string">&#x27;a&#x27;</span>, <span class="hljs-string">&#x27;b&#x27;</span>], fileData</span>)</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self, a, b</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\n&#x27;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; a: <span class="hljs-subst">&#123;a&#125;</span>&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; b: <span class="hljs-subst">&#123;b&#125;</span>&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 3 items</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a[1-2]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 1</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[2-3]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 2</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[3-4]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 3</span><br><span class="hljs-string">data --&gt; b: 4</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>定义多个参数时, 可以使用 <strong>数组/元组/字符串</strong></p>
<p>字符串的使用方式, 参数之间需要用 <strong>逗号</strong> 隔开, 如: ‘a, b’</p>
<p>为了避免编写方面的错误, 建议使用 <strong>数组/元组</strong></p>
<p>可以发现, 在使用多参数时, 被测数据需要能够支持 <strong>参数解包</strong></p>
<p>否则会报错! 这个逻辑就像python本身的解包功能一样.</p>
<h2 id="驱动测试类"><a href="#驱动测试类" class="headerlink" title="驱动测试类"></a>驱动测试类</h2><p>驱动一个测试类时, 会将数据集合传递给类的所有测试方法</p>
<p>每个测试方法, 都将有对应的参数可调用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-comment"># 模拟文件内容, 将文件内容按行拆分</span><br>fileData = [<br>    (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>),<br>    (<span class="hljs-number">2</span>, <span class="hljs-number">3</span>),<br>]<br><br><span class="hljs-meta">@pytest.mark.parametrize(<span class="hljs-params"><span class="hljs-string">&#x27;a, b&#x27;</span>, fileData</span>)</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self, a, b</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\n&#x27;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; a: <span class="hljs-subst">&#123;a&#125;</span>&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; b: <span class="hljs-subst">&#123;b&#125;</span>&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_b</span>(<span class="hljs-params">self, a, b</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;\n&#x27;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; a: <span class="hljs-subst">&#123;a&#125;</span>&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; b: <span class="hljs-subst">&#123;b&#125;</span>&quot;</span>)<br>        <span class="hljs-keyword">assert</span> <span class="hljs-number">1</span><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 4 items</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a[1-2]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 1</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[2-3]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 2</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_b[1-2]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 1</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_b[2-3]</span><br><span class="hljs-string"></span><br><span class="hljs-string">data --&gt; a: 2</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<h2 id="多个装饰器"><a href="#多个装饰器" class="headerlink" title="多个装饰器"></a>多个装饰器</h2><p>这种用法, 主要是为了进行组合运行</p>
<p>被多个装饰器装饰时, 运行次数等于: m * n</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> pytest<br><br><span class="hljs-comment"># 模拟文件内容, 将文件内容按行拆分</span><br>fileData1 = [<br>    <span class="hljs-number">1</span>,<br>    <span class="hljs-number">2</span>,<br>    <span class="hljs-number">3</span><br>]<br><br>fileData2 = [<br>    <span class="hljs-number">2</span>,<br>    <span class="hljs-number">3</span><br>]<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">TestCase</span>():<br><br><span class="hljs-meta">    @pytest.mark.parametrize(<span class="hljs-params"><span class="hljs-string">&#x27;a&#x27;</span>, fileData1</span>)</span><br><span class="hljs-meta">    @pytest.mark.parametrize(<span class="hljs-params"><span class="hljs-string">&#x27;b&#x27;</span>, fileData2</span>)</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_a</span>(<span class="hljs-params">self, a, b</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;&#x27;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; a: <span class="hljs-subst">&#123;a&#125;</span>&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;data --&gt; b: <span class="hljs-subst">&#123;b&#125;</span>&quot;</span>)<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;a(<span class="hljs-subst">&#123;a&#125;</span>) ** b<span class="hljs-subst">&#123;b&#125;</span>: <span class="hljs-subst">&#123;a ** b&#125;</span>&quot;</span>)<br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&#x27;__main__&#x27;</span>:<br>    pytest.main([<span class="hljs-string">&#x27;-s&#x27;</span>, <span class="hljs-string">&#x27;-v&#x27;</span>, __file__])  <span class="hljs-comment"># 加上 -m 控制需要运行的标签用例</span><br><br><span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">collected 6 items</span><br><span class="hljs-string"></span><br><span class="hljs-string">test3.py::TestCase::test_a[2-1]</span><br><span class="hljs-string">data --&gt; a: 1</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">a(1) ** b2: 1</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[2-2]</span><br><span class="hljs-string">data --&gt; a: 2</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">a(2) ** b2: 4</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[2-3]</span><br><span class="hljs-string">data --&gt; a: 3</span><br><span class="hljs-string">data --&gt; b: 2</span><br><span class="hljs-string">a(3) ** b2: 9</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[3-1]</span><br><span class="hljs-string">data --&gt; a: 1</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">a(1) ** b3: 1</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[3-2]</span><br><span class="hljs-string">data --&gt; a: 2</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">a(2) ** b3: 8</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">test3.py::TestCase::test_a[3-3]</span><br><span class="hljs-string">data --&gt; a: 3</span><br><span class="hljs-string">data --&gt; b: 3</span><br><span class="hljs-string">a(3) ** b3: 27</span><br><span class="hljs-string">PASSED</span><br><span class="hljs-string">&quot;&quot;&quot;</span><br></code></pre></td></tr></table></figure>

<p>可以发现, 总共运行了6次</p>
<p>因为fileData1为三组数据, fileData2为两组数据, 所以共运行: m(3) * n(2) = 6 次</p>
<p>从结果可以看出, 符合我们的用例运行结果, 分别求出三组数据的 <strong>二次幂/三次幂</strong></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/" class="category-chain-item">自动化测试</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/pytest/" class="category-chain-item">pytest</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/">#自动化测试</a>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95%E6%A1%86%E6%9E%B6/">#自动化测试框架</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>pytest进阶使用</div>
      <div>https://walker_kwu.gitee.io/my-blog/2023/05/11/autoTest/pytest进阶使用/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年5月11日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2023/05/12/autoTest/pytest%E5%BF%85%E5%AD%A6%E4%B9%8Bfixture/" title="pytest必学之fixture">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">pytest必学之fixture</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2023/05/10/autoTest/pytest%E5%B8%B8%E7%94%A8%E6%8F%92%E4%BB%B6/" title="pytest常用插件">
                        <span class="hidden-mobile">pytest常用插件</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
